<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>修改医生信息</title>
    <link href="/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>

<div class="container mt-5">
    <h2>修改医生信息</h2>
    <form id="updateDoctorForm">
        <input type="hidden" id="id" name="id">
        <div class="mb-3">
            <label for="departmentId" class="form-label">科室ID</label>
            <input type="number" class="form-control" id="departmentId" name="departmentId" required>
        </div>
        <div class="mb-3">
            <label for="name" class="form-label">医生姓名</label>
            <input type="text" class="form-control" id="name" name="name" required>
        </div>
        <div class="mb-3">
            <label for="title" class="form-label">职称</label>
            <input type="text" class="form-control" id="title" name="title">
        </div>
        <div class="mb-3">
            <label for="description" class="form-label">描述</label>
            <textarea class="form-control" id="description" name="description"></textarea>
        </div>
        <div class="mb-3">
            <label for="workingDays" class="form-label">出诊日期</label>
            <input type="text" class="form-control" id="workingDays" name="workingDays">
        </div>
        <div class="row mb-3">
            <div class="col">
                <label for="morningStart" class="form-label">上午开始</label>
                <input type="time" class="form-control" id="morningStart" name="morningStart">
            </div>
            <div class="col">
                <label for="morningEnd" class="form-label">上午结束</label>
                <input type="time" class="form-control" id="morningEnd" name="morningEnd">
            </div>
        </div>
        <div class="row mb-3">
            <div class="col">
                <label for="afternoonStart" class="form-label">下午开始</label>
                <input type="time" class="form-control" id="afternoonStart" name="afternoonStart">
            </div>
            <div class="col">
                <label for="afternoonEnd" class="form-label">下午结束</label>
                <input type="time" class="form-control" id="afternoonEnd" name="afternoonEnd">
            </div>
        </div>
        <div class="mb-3">
            <label for="status" class="form-label">状态</label>
            <select class="form-select" id="status" name="status">
                <option value="1">启用</option>
                <option value="0">禁用</option>
            </select>
        </div>
        <button type="submit" class="btn btn-primary">保存修改</button>
    </form>
</div>

<script src="/js/jquery.min.js"></script>
<script>
    $(document).ready(function () {
        const urlParams = new URLSearchParams(window.location.search);
        const doctorId = urlParams.get('id');

        if (!doctorId) {
            alert("缺少医生ID");
            return;
        }

        $.get("/admins/getAllDoctor", function (doctors) {
            const doctor = doctors.find(d => d.id == doctorId);
            if (!doctor) {
                alert("未找到医生");
                return;
            }

            $('#id').val(doctor.id);
            $('#departmentId').val(doctor.departmentId);
            $('#name').val(doctor.name);
            $('#title').val(doctor.title);
            $('#description').val(doctor.description);
            $('#workingDays').val(doctor.workingDays);
            $('#morningStart').val(doctor.morningStart);
            $('#morningEnd').val(doctor.morningEnd);
            $('#afternoonStart').val(doctor.afternoonStart);
            $('#afternoonEnd').val(doctor.afternoonEnd);
            $('#status').val(doctor.status);
        });

        $('#updateDoctorForm').on('submit', function (e) {
            e.preventDefault();

            const updatedDoctor = {
                id: parseInt($('#id').val()),
                departmentId: parseInt($('#departmentId').val()),
                name: $('#name').val(),
                title: $('#title').val(),
                description: $('#description').val(),
                workingDays: $('#workingDays').val(),
                morningStart: $('#morningStart').val(),
                morningEnd: $('#morningEnd').val(),
                afternoonStart: $('#afternoonStart').val(),
                afternoonEnd: $('#afternoonEnd').val(),
                status: parseInt($('#status').val())
            };

            $.ajax({
                url: "/admins/updateDoctor",
                type: "PUT",
                contentType: "application/json",
                data: JSON.stringify(updatedDoctor),
                success: function () {
                    alert("医生信息已更新");
                    window.location.href = "/doctorList";
                },
                error: function () {
                    alert("更新失败");
                }
            });
        });
    });
</script>

</body>
</html>
